package com.nginxmanager.server.serverIpm;


import com.nginxmanager.domain.Proxy;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-01-05.
 * 监测原服务器是否宕机
 */
public class HttpDoctor {
    public Boolean doctor(Proxy proxy){
        HttpURLConnection httpUrlConn = null;
        Map<String,List<String>> map=null;
        // 建立get请求
        URL url = null;
        try {
            if (proxy.getPort()==null){
                url = new URL("http://"+proxy.getIp());
            }else {
                url = new URL("http://" + proxy.getIp() + ":" + proxy.getPort());
            }
            System.out.println("服务器地址:"+url);
        } catch (MalformedURLException e) {
            System.out.println("程序出错请重试！"+url);
            e.printStackTrace();
        }
        try {
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            map = httpUrlConn.getHeaderFields();
            System.out.println("服务器返回:"+map.toString());
            if (!map.isEmpty()){
                System.out.println("连接成功，敲级开森");
                return true;
            }
        } catch (IOException e) {
            System.out.println("服务器连接失败返回:"+map.toString());
            return false;
        }
        return false;
    }
}
